package com.siemens.industrial.utils;

import java.io.*;

/**
 * Created by qupen on 2016/9/22.
 */
public class SiemensFileWriter {
    public static boolean writeToFile(String fileName, String content, boolean append) {
        File file = new File(fileName);

        try (FileOutputStream fos = new FileOutputStream(file, append)) {

            // if file doesn't exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            // get the content in bytes
            byte[] contentInBytes = content.getBytes();

            fos.write(contentInBytes);
            fos.flush();
            fos.close();

            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

    public static boolean writeToFile(String filePath, String fileName, String content, boolean append) {
        File logDir = new File(filePath);

        try {
            if (!logDir.exists() && !logDir.isDirectory()) {
                logDir.mkdirs();
            }

            File logFile = new File(filePath + "\\" + fileName+".txt");

            if (!logFile.exists()) {
                logFile.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(filePath + "\\" + fileName+".txt", append);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
            osw.write(content+"\r\n");
            osw.close();
            fos.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

    public static boolean checkPath(String path){
        File file =new File(path);
        if (file.exists()){
            return true;
        }
        return false;
    }
}
